package com.vpx.server.controller.system;

import com.vpx.server.bean.entity.SysRole;
import com.vpx.server.bean.info.system.MenuTreeInfo;
import com.vpx.server.framework.authority.AuthPoicy;
import com.vpx.server.framework.authority.RequestAuth;
import com.vpx.server.framework.authority.SessionUtil;
import com.vpx.server.bean.order.base.BaseDeleteOrder;
import com.vpx.server.bean.result.system.SysMenuResult;
import com.vpx.server.framework.base.ResultMsg;
import com.vpx.server.service.system.AuthorityService;
import com.vpx.server.service.system.SysMenuService;
import com.vpx.server.bean.order.base.BaseIdOrder;
import com.vpx.server.bean.order.system.SysMenuOrder;
import com.vpx.server.bean.result.base.BaseDeleteResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liuks
 * @title: vpx
 * @module 菜单管理
 * @description: 菜单管理接口
 * @date 2021/9/7 19:15
 */
@RestController
@RequestMapping(value = "/api/menu")
@Api(tags = "基础设置-菜单管理")
public class SysMenuController {

    @Autowired
    private SysMenuService sysMenuService;
    @Autowired
    AuthorityService authorityService;

    @PostMapping("insert")
    @ApiOperation("新增菜单")
    @RequestAuth(poicy = AuthPoicy.AUTH)
    public ResultMsg insert(@RequestBody SysMenuOrder order) {
       return sysMenuService.insert(order);
    }

    @DeleteMapping("deleteById")
    @ApiOperation("删除菜单")
    @RequestAuth(poicy = AuthPoicy.AUTH)
    public ResultMsg<BaseDeleteResult> deleteById(@RequestBody BaseDeleteOrder order) {
        return sysMenuService.deleteById(order);
    }

    @PutMapping("updateById")
    @ApiOperation("修改菜单")
    @RequestAuth(poicy = AuthPoicy.AUTH)
    public ResultMsg updateById(@RequestBody SysMenuOrder order) {
        return sysMenuService.updateById(order);
    }

    @GetMapping("findById")
    @ApiOperation("菜单查询")
    @RequestAuth(poicy = AuthPoicy.AUTH)
    public ResultMsg<SysMenuResult> findById(BaseIdOrder order) {
        return sysMenuService.findById(order);
    }

    /*@GetMapping("listQuery")
    @ApiOperation("菜单列表查询")
    @RequestAuth(poicy = AuthPoicy.AUTH)
    public ReplyJson<SysMenuListResult> listQuery(SysMenuParentIdOrder order) {
        SysMenuListResult result = sysMenuService.findListByParentId(order);
        return result.isSuccess() ? ReplyJson.success("查询成功", result) : ReplyJson.fail(result.getMessage());
    }*/

    /**
     * 这里需要设置为LOGIN
     * @return
     */
    @GetMapping("treeQuery")
    @RequestAuth(poicy = AuthPoicy.LOGIN)
    @ApiOperation("菜单树形结构")
    public ResultMsg<List<MenuTreeInfo>> treeQuery() {
        List<MenuTreeInfo> treeList = authorityService.getMenuTree().getData();
        return ResultMsg.success(treeList);
    }

    @GetMapping("getMenuTreeAndOperate")
    @RequestAuth(poicy = AuthPoicy.AUTH)
    @ApiOperation("菜单树形结构及菜单按钮")
    public ResultMsg<List<MenuTreeInfo>> getMenuTreeAndOperate() {

        List<SysRole> roleList = SessionUtil.getSession().getSessionUser().getRoleList();
        List<Long> roleIdList = new ArrayList<Long>();
        if(roleList != null){
            roleList.forEach(o -> {
                roleIdList.add(o.getId());
            });
        }

        List<MenuTreeInfo> treeList = authorityService.getMenuTreeAndOperate(roleIdList).getData();
        return ResultMsg.success(treeList);
    }


}
